/*  面包屑导航   */
export function breadcrumb(router) {

  let routeList = JSON.parse(sessionStorage.getItem("routeList")) || [];
  router.beforeEach((to, from, next) => {

    // 数组去重
    if (routeList.length > 0) {
      let obj = {};
      routeList = routeList.reduce(function (item, next) {
        obj[next.path] ? "" : (obj[next.path] = true && item.push(next));
        return item;
      }, []);
    }

    let index = -1;
    for (let i = 0; i < routeList.length; i++) {
      if (routeList[i].name == to.meta.name) {
        index = i;
        break;
      }
    }
    let nameArr = ["主页", "菜单管理", "用户管理", "个人中心", "文章管理", "轮播管理"];
    // "权限设置"
    for (let i in nameArr) {
      if (nameArr[i] == to.meta.name) {
        routeList = [];
        index = -1;
        break;
      }
    }

    if (index !== -1) {
      //如果存在路由列表，则把之后的路由都删掉
      routeList.splice(index + 1, routeList.length - index - 1);
    } else if (to.meta.name != "登录") {
      // 如果不存在路由列表, 则利用元信息拼装一个路由列表
      routeList.push({name: to.meta.name, path: to.fullPath});
    }
    to.meta.routeList = routeList;
    sessionStorage.setItem("routeList", JSON.stringify(routeList));
    next();
  });
}
